package com.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dao.Log;
import com.dao.Users;
import com.util.MySqlSessionFactory;

@Controller
public class UsersController {
	SqlSession sql = MySqlSessionFactory.getSqlSession();

	@RequestMapping(value = "/register.action", method = RequestMethod.POST)
	@ResponseBody
	public String register(
			@RequestParam(value = "account", defaultValue = "") String account,
			@RequestParam(value = "password", defaultValue = "") String password,
			@RequestParam(value = "password2", defaultValue = "") String password2) {
		Users dao = sql.getMapper(Users.class);
		Log Logdao = sql.getMapper(Log.class);
		Map<String, Object> result = dao.query(account);
		if (!password.equals(password2)) {
			return "ERROR_PASSWORD2";
		}
		if ("1".equals(result.get("counter").toString())) {
			return "ERR";
		}
		dao.add(account, password, "2012-12-12");
		Map<String, Object> map = dao.queryid();
		Logdao.addrecord1(map.get("id").toString(), "0", "注册");
		sql.commit();
		return "OK";
	}

	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	@ResponseBody
	public String login(
			@RequestParam(value = "account", defaultValue = "") String account,
			@RequestParam(value = "password", defaultValue = "") String password,
			HttpServletResponse response) {
		Users dao = sql.getMapper(Users.class);
		Log Logdao = sql.getMapper(Log.class);
		Map<String, Object> result = dao.queryone(account, password);

		if ("0".equals(result.get("counter").toString())) {
			return "no";
		}
		response.addCookie(new Cookie("uu", result.get("id").toString()));
		// System.out.println(result);
		Logdao.addrecord1(result.get("id").toString(), "0", "登陆");
		sql.commit();
		return "yes";
	}

	// if("".equals(uuId)){
	// response.sendRedirect("login.html");
	// System.out.println("[error] getuuInfo.action: redirect\r\n");
	// }

	@RequestMapping(value = "/users.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> users(
			@CookieValue(value = "uu", defaultValue = "") String uuId,
			HttpServletResponse response) {
		Users dao = sql.getMapper(Users.class);
		Map<String, Object> map = dao.queryusers(uuId);
		sql.commit();
		// System.out.println(map);
		// sql.close();
		return map;
	}

	@RequestMapping(value = "/money.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> money(
			@CookieValue(value = "uu", defaultValue = "") String uuId,
			HttpServletResponse response) {
		Users dao = sql.getMapper(Users.class);
		Map<String, Object> map = dao.querymoney(uuId);
		sql.commit();
		// System.out.println(map);
		return map;
	}

	@RequestMapping(value = "/jinqi.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, Object>> jinji(
			@CookieValue(value = "uu", defaultValue = "") String uuId,
			HttpServletResponse response) {
		Users dao = sql.getMapper(Users.class);
		List<Map<String, Object>> list = dao.queryjinqi(uuId);
		sql.commit();
		return list;
	}

	@RequestMapping(value = "/jinqi1.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, Object>> jinqi1(
			@CookieValue(value = "uu", defaultValue = "") String uuId,
			HttpServletResponse response) {
		Users dao = sql.getMapper(Users.class);
		List<Map<String, Object>> list = dao.queryjinqi1(uuId);
		sql.commit();
		return list;
	}

	@RequestMapping(value = "/outadd.action", method = RequestMethod.POST)
	public String outadd(

			@CookieValue(value = "uu", defaultValue = "") String uuId,

			@RequestParam(value = "place", defaultValue = "") String place,
			@RequestParam(value = "money_out", defaultValue = "") String money_out,
			@RequestParam(value = "kind", defaultValue = "") String kind,
			@RequestParam(value = "remark") String remark,
			HttpServletResponse response) {
		Users dao = sql.getMapper(Users.class);
		Log Logdao = sql.getMapper(Log.class);
		dao.addout(uuId, "2016-07-14", place, money_out, kind, remark);
		Map<String, Object> map = dao.queryid();
		// System.out.println("123");
		Logdao.addrecord1(uuId, map.get("id").toString(), "支出");
		sql.commit();
		return "redirect:homepage.html";
	}

	@RequestMapping(value = "/inadd.action", method = RequestMethod.POST)
	@ResponseBody
	public String inadd(
			@CookieValue(value = "uu", defaultValue = "") String uuId,
			@RequestParam(value = "date_time") String date_time,
			@RequestParam(value = "place", defaultValue = "") String place,
			@RequestParam(value = "money_in", defaultValue = "") String money_in,
			@RequestParam(value = "kind", defaultValue = "") String kind,
			@RequestParam(value = "remark") String remark,
			HttpServletResponse response) {
		Users dao = sql.getMapper(Users.class);
		Log Logdao = sql.getMapper(Log.class);
		Map<String, Object> map = dao.queryid();
		dao.addin(uuId, date_time, place, money_in, kind, remark);
		Logdao.addrecord1(uuId, map.get("id").toString(), "收入");
		sql.commit();
		return "OK";
	}

	@RequestMapping(value = "/stamonth.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> month(
			@CookieValue(value = "uu", defaultValue = "") String uuId,
			HttpServletResponse response) {
		Users dao = sql.getMapper(Users.class);
		Map<String, Object> map = dao.querymonth(uuId);
		sql.commit();
		return map;
	}

	@RequestMapping(value = "/stakind.action", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, Object>> kind(
			@CookieValue(value = "uu", defaultValue = "") String uuId,
			HttpServletResponse response) {
		Users dao = sql.getMapper(Users.class);
		List<Map<String, Object>> list = dao.querykind(uuId);
		sql.commit();
		return list;
	}

	@RequestMapping(value = "/search.action", method = RequestMethod.POST)
	@ResponseBody
	public List<Map<String, Object>> serach(
			@RequestParam(value = "date_time", defaultValue = "") String date_time,
			@RequestParam(value = "place", defaultValue = "") String place,
			@RequestParam(value = "money_in", defaultValue = "") String money_in,
			@RequestParam(value = "money_out", defaultValue = "") String money_out,
			@RequestParam(value = "kind", defaultValue = "") String kind,
			@RequestParam(value = "remark", defaultValue = "") String remark,
			@CookieValue(value = "uu", defaultValue = "") String uuId) {
		Users dao = sql.getMapper(Users.class);
		Log Logdao = sql.getMapper(Log.class);
		Map<String, Object> map = dao.queryid();
		Logdao.addrecord1(uuId, map.get("id").toString(), "查询");
		List<Map<String, Object>> list = dao.queryevery(uuId, date_time, place,
				money_in, money_out, kind, remark);
		sql.commit();
		return list;
	}

	@RequestMapping(value = "/quit.action", method = RequestMethod.POST)
	@ResponseBody
	public String quit(
			@CookieValue(value = "uu", defaultValue = "") String uuId,
			HttpServletResponse response) {
		Users dao = sql.getMapper(Users.class);
		Log Logdao = sql.getMapper(Log.class);
		Map<String, Object> map = dao.queryid();
		Logdao.addrecord1(uuId, map.get("id").toString(), "退出");
		sql.commit();
		response.addCookie(new Cookie("uu", null));
		return "q";
	}

	@RequestMapping(value = "/details.action", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> details1(
			@CookieValue(value = "uu", defaultValue = "") String uuId,
			@RequestParam(value = "id", defaultValue = "") String ID,
			HttpServletResponse response) {
		Users dao = sql.getMapper(Users.class);
		Log Logdao = sql.getMapper(Log.class);
		Map<String, Object> map = dao.queryid();
		Map<String, Object> map1 = dao.querydetails(ID, uuId);
		Logdao.addrecord1(uuId, map.get("id").toString(), "查询详情");
		// System.out.println(map);
		// System.out.println(id);
		// System.out.println(uuId);
		sql.commit();
		return map1;
	}

	@RequestMapping(value = "/del.action", method = RequestMethod.POST)
	@ResponseBody
	public String del(
			@CookieValue(value = "uu", defaultValue = "") String uuId,
			@RequestParam(value = "id", defaultValue = "") String ID,
			HttpServletResponse response) {
		Users dao = sql.getMapper(Users.class);
		Log Logdao = sql.getMapper(Log.class);
		Map<String, Object> map = dao.queryid();
		dao.del(ID, uuId);
		Logdao.addrecord1(uuId, map.get("id").toString(), "删除");
		sql.commit();
		return "w";
	}

}
